package com.gurun.codes.business.temperature_humidity;

import android.content.Context;

import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.contract.base.BasePresenter;
import com.gurun.codes.contract.base.view.RxView;
import com.gurun.codes.utils.Constants;
import com.gurun.model.base.ResultObserver;
import com.gurun.model.base.RetrofitManager;
import com.gurun.model.bean.ApiResult;
import com.gurun.model.bean.BaseRequestBean;
import com.gurun.model.bean.home.GetHomeDeviceListRequestBean;
import com.gurun.model.bean.home.GetHomeDeviceListResultBean;
import com.gurun.model.bean.home.GetHomeListResultBean;
import com.gurun.model.bean.temperature_humidity.GetTemperatureHumidityInfoRequestBean;
import com.gurun.model.bean.temperature_humidity.GetTemperatureHumidityInfoResultBean;
import com.gurun.model.service.HomeService;
import com.gurun.model.service.TemperatureHumidityService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface TemperatureHumidityActivityContract {

    interface TemperatureHumidityActivityView extends RxView {

        void onLoading();

        void onLoaded(GetTemperatureHumidityInfoResultBean datas);

        void onError(String msg);

        void onComplete();
    }

    class TemperatureHumidityActivityViewPresenter extends BasePresenter<TemperatureHumidityActivityView> {

        private Context mContext;

        public TemperatureHumidityActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        public void getTemperatureHumidityInfo(long deviceId,int timeFlag) {
            GetTemperatureHumidityInfoRequestBean requestBean = new GetTemperatureHumidityInfoRequestBean();
            requestBean.setVersion(Constants.HTTP_VERSION);
            requestBean.setUserId(App.getInstance().getLoginUserId());
            requestBean.setToken(App.getInstance().getLoginToken());
            requestBean.setPageNo(1);
            requestBean.setPageSize(1000);
            requestBean.setRefreshTime("0");
            requestBean.setDeviceId(deviceId);
            requestBean.setTimeFlag(timeFlag);

            RetrofitManager.getInstance().createApiService(TemperatureHumidityService.class)
                    .getTemperatureHumidityInfo(requestBean)
                    .compose(getView().<GetTemperatureHumidityInfoResultBean>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new ResultObserver<GetTemperatureHumidityInfoResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_connect_server_failed));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(GetTemperatureHumidityInfoResultBean result, String msg) {
                            getView().onLoaded(result);
                        }

                        @Override
                        public void onApiError(ApiResult result) {
                            if (result != null) {
                                getView().onError(result.getRetMsg());
                            } else {
                                getView().onError(mContext.getString(R.string.common_connect_server_failed));
                            }
                        }
                    }));
        }
    }
}
